/*
 *	Source: http://www.yolinux.com/TUTORIALS/XML-Xerces-C.html
 */

#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>


#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include "XMLParser.h"

using namespace xercesc;
using namespace std;


/**
 *  - Tests the access and availability of the XML configuration file.
 *  - Configures the xerces-c DOM parser.
 *  - Reads and extracts the pertinent information from the XML config file.
 * @param const char * configFile - filename
 * @param std::vector<Class*>& vecResult - information is stored here
 * @return void
 */
void XMLParser::readClassConfigFile(const char * configFile, std::vector<Class*>& vecResult)
{
	try
	{
		if (checkFile(configFile) == -1)
		{
			throw(std::runtime_error( "XML document not available" ));
		}
		m_ConfigFileParser->parse( configFile );
		DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
		DOMElement* elementRoot = xmlDoc->getDocumentElement();
		if( !elementRoot )
		{
			throw(std::runtime_error( "empty XML document" ));
		}
		if (!XMLString::equals(elementRoot->getTagName(), m_TAG_root))
		{
			throw(std::runtime_error( "Bad root tag for this config file." ));
		}
		DOMNodeList* childrens = elementRoot->getElementsByTagName(m_TAG_ApplicationSettings);
		const  XMLSize_t nodeCount = childrens->getLength();
		for( XMLSize_t i = 0; i < nodeCount; ++i )
		{
			DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >(childrens->item(i));
			if (m_ATTR_Options.size() == XMLPARSER_NUMOPTIONS_CLASSCONFIG)
			{
				Class* newClass = new Class;
				vector<const XMLCh*> xmlch_Option;

				for (unsigned int k = 0; k < m_ATTR_Options.size(); ++k)
				{
					xmlch_Option.push_back(currentElement->getAttribute(m_ATTR_Options[k]));
				}
				newClass->setName(XMLString::transcode(xmlch_Option[0]));
				newClass->setMinimunReq(atoi(XMLString::transcode(xmlch_Option[1])),
										atoi(XMLString::transcode(xmlch_Option[2])),
										atoi(XMLString::transcode(xmlch_Option[3])),
										atoi(XMLString::transcode(xmlch_Option[4])),
										atoi(XMLString::transcode(xmlch_Option[5])));
				newClass->setHPMP(atoi(XMLString::transcode(xmlch_Option[6])),
								  atoi(XMLString::transcode(xmlch_Option[7])),
								  atoi(XMLString::transcode(xmlch_Option[8])),
								  atoi(XMLString::transcode(xmlch_Option[9])));
				newClass->setCombatFactors(atoi(XMLString::transcode(xmlch_Option[10])),
											atoi(XMLString::transcode(xmlch_Option[11])),
											atoi(XMLString::transcode(xmlch_Option[12])),
											atoi(XMLString::transcode(xmlch_Option[13])));
				newClass->setEvasion(atoi(XMLString::transcode(xmlch_Option[14])));
				newClass->setDiscretion(atoi(XMLString::transcode(xmlch_Option[15])),
										atoi(XMLString::transcode(xmlch_Option[16])));
				newClass->setGold(atoi(XMLString::transcode(xmlch_Option[17])),
								  atoi(XMLString::transcode(xmlch_Option[18])));
				newClass->setDesc(XMLString::transcode(xmlch_Option[19]));

				DOMNodeList* childrens2 = currentElement->getElementsByTagName(m_TAG_ApplicationSettings2);
				const  XMLSize_t nodeCount2 = childrens2->getLength();
				for( XMLSize_t j = 0; j < nodeCount2; ++j )
				{
					DOMElement* currentElement2 = dynamic_cast< xercesc::DOMElement* >(childrens2->item(j));
					const XMLCh* xmlch_OptionA = currentElement2->getAttribute(m_ATTR_Options[0]);
					
					newClass->addItem(XMLString::transcode(xmlch_OptionA));
				}
				vecResult.push_back(newClass);
			}
		}
	}
	catch( xercesc::XMLException& e )
	{
		char* message = xercesc::XMLString::transcode( e.getMessage() );
		ostringstream errBuf;
		errBuf << "Error parsing file: " << message << flush;
		XMLString::release( &message );
	}
}
